import { request } from "../../request/index"
// pages/category/index.js
import regeneratorRuntime from "../../lib/runtime/runtime";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftMenuList:[],
    rightContentList:[],
    currentIndex:0
  },
  Cates:[],
  scrollTop:0,

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let Cates = wx.getStorageSync('cates');
    if(Cates && (Date.now() - Cates.time) < 10*1000){
      this.Cates = Cates.data
      let leftMenuList = this.Cates.map(v=>v.cat_name)
      let rightContentList = this.Cates[0].children
      this.setData({
        leftMenuList,
        rightContentList
      })
      return
    }
      this.getMenuList();
  },
  async getMenuList(){
    this.Cates = await request({
      url:'/categories'
    })
      wx.setStorageSync('cates', {time:Date.now(),data:this.Cates});
      let leftMenuList = this.Cates.map(v=>v.cat_name)
      let rightContentList = this.Cates[0].children
      this.setData({
        leftMenuList,
        rightContentList
      })
    
  },
  handleItemTap(e){
    const {index} = e.target.dataset;
    let rightContentList = this.Cates[index].children
      this.setData({
        currentIndex:index,
        rightContentList,
        scrollTop:0
      })
    
  }

})